package com.hhwy.framework.code.generation;

import java.io.IOException;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.util.StatusPrinter;

import com.hhwy.framework.code.generation.swing.Config;
import com.hhwy.framework.code.generation.util.CodeGenerationUtil;
import com.hhwy.framework.code.generation.util.PomUtil;
import com.hhwy.framework.code.generation.util.PropertyUtil;

/**
 * 代码生成主程序
 * @author zhangbaojian
 * @version 1.0
 * @created 21-7月-2014 9:24:59
 */
public class CodeGenerationMain {

	public static final Logger log = LoggerFactory
			.getLogger(CodeGenerationMain.class);

	static {
		initLogConfig();
		PropertyUtil.initProperties();
		PomUtil.init();
	}
	
	public static void main(String[] args) {
		executeCodeGeneration();
	}

	/**
	 * 
	 * 默认通过配置文件codeConfig.properties的配置生成代码
	 * 如果配置文件不存在，则通过db.properties加载数据库，通过界面输入配置参数：表名，包名，场景名生成代码
	 * @exception 
	 * @since  1.0.0
	 */
	public static void executeCodeGeneration() {
		try {
			if(PropertyUtil.config == null){
				new Config("代码生成配置信息");
			} else {
				String codeGenerationPath = PropertyUtil.getValue(
						"code.generation.path");
				String packagePath = PropertyUtil.getValue("package.path");
				String sceneName = PropertyUtil.getValue("scene.name");
				String tableName = PropertyUtil.getValue("table.name");
				loopCreateCode(codeGenerationPath, packagePath, sceneName, tableName);
			}
		} catch (Exception e) {
			log.error("文件配置错误", e);
			e.printStackTrace();
		}

	}

	public static void loopCreateCode(String codePath,String codePackagePath,String codeSceneName,String codeTableName) {
		String[] codeGenerationPath = codePath.split(",");
		String[] packagePath = codePackagePath.split(",");
		String[] sceneName = codeSceneName.split(",");
		String[] tableName = codeTableName.split(",");
		int j = 0, k = 0;
		for (int i = 0; i < tableName.length; i++) {
			if (codeGenerationPath.length > 1) {
				j++;
			}
			if (packagePath.length > 1) {
				k++;
			}
			createCode(codeGenerationPath[j], packagePath[k], sceneName[i],
					tableName[i]);
		}
		log.info("代码生成完成");
	}

	private static void createCode(String codeGenerationPath,
			String packagePath, String sceneName, String tableName) {
		CodeGenerationUtil code = new CodeGenerationUtil();
		code.init(codeGenerationPath, packagePath, sceneName, tableName);
		try {
			//后台代码生成
			code.createController();
			code.createService();
			code.createDomain();
			//code.createControllerTest();
			//code.createServiceTest();
			
			//创建映射文件
			code.createMappers();
			
			//前端 minu UI生成
			code.createList();
			code.createAdd();
			code.createEdit();
			code.createDetail();
			System.out.println("代码生成结束");
		} catch (Exception e) {
			log.error("生成代码文件失败", e);
		}
	}

	public static void initLogConfig() {
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(lc);
		lc.reset();
		try {
			URL url = new ClassPathResource("/logback.xml").getURL();
			configurator.doConfigure(url);
			StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
			log.info("加载logback.xml配置文件成功");
		} catch (Exception e) {
			System.out.println("加载logback.xml配置文件失败");
			e.printStackTrace();
		}
	}
}